package com.zzgc.modules.controller;

import com.zzgc.common.entity.AuthUser;
import com.zzgc.common.entity.School;
import com.zzgc.common.entity.SchoolGrade;
import com.zzgc.common.entity.Student;
import com.zzgc.common.response.HttpStatus;
import com.zzgc.common.response.Result;
import com.zzgc.common.utils.MergePdfFiles;
import com.zzgc.common.utils.ZxingUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:
 * @CreateTime: 2023-01-13  10:56
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("school")
@Api(tags = "学校管理")
public class SchoolController extends BaseController{

    @GetMapping("getList")
    @ApiOperation(value = "查询学校", notes = "列表展示, 查询学校")
    public Result getSchools(School school){
        // 根据角色查询学校
        if (super.getDepartId() != null) {
            school.setSDepartId(Long.valueOf(super.getDepartId()));
        }else {
            return Result.builder().code(500).msg("暂无部门信息！").build();
        }
        List<School> schools = schoolService.queryByPage(school);
        HashMap<String, Object> map = new HashMap<>();
        // 添加总条数
        map.put("total", schools.size() == 0 ? 0 : schools.get(0).getTotal());
        // 添加学校人数
        for (School school1 : schools) {
            Student student = new Student();
            student.setSSchoolId(school1.getId());
            // 得设置年份
            student.setSYear(getsYear() + "");
            long count = studentService.count(student);
            school1.setSStudentNum((int) count);
        }
        map.put("data", schools);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(map).build();
    }

    @GetMapping("print")
    @ApiOperation(value = "打印全校二维码", notes = "打印全校二维码")
    public Result printSchoolAll(Long gradeId){
        Student student = new Student();
        List<Student> arrayList = new ArrayList<>();
        student.setSSchoolId(Long.valueOf(super.getSchoolId()));
        if (gradeId != null){
            student.setSGradeId(gradeId);
        }
        student.setSize(Integer.MAX_VALUE);
        List<Student> students = studentService.queryByPage(student);
        File file = new File(qrcodePath); // 没有此路径就创建
        if (!file.exists()) {
            file.mkdirs();
        }
        for (Student student1 : students) {
            String sIdCard = student1.getSIdCard();
            String codePath = qrcodePath + sIdCard + ".jpg";
            // 根据学生唯一ID, 生成二维码
            ZxingUtils.getQRCodeImge(String.valueOf(student1.getSStudentId()), 200, codePath);
           // 拼接二维码路径
            student1.setTableName("/oss/qrcode/" + sIdCard + ".jpg");
            arrayList.add(student1);
        }
        // 合并图片, 每6张合成一张
        List<List<Student>> lists = MergePdfFiles.mySubList(arrayList);
        return Result.builder()
                .code(HttpStatus.OK.value())
                .msg(HttpStatus.OK.getReasonPhrase())
                .data(lists)
                .build();
    }
}
